package com.ffcs.crmd.tsp.client.log;

import java.lang.reflect.Method;
import java.net.URL;

import org.slf4j.ILoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ffcs.crmd.tsp.core.util.MixAll;

/**
 * 
 * 功能说明:客户端日志
 *
 * @author zhongfuhua
 * 
 * @Date 2016年2月3日 上午1:20:19
 *
 *
 * 版本号  |   作者   |  修改时间   |   修改内容
 *
 */
public class ClientLogger {
    
    protected ClientLogger() {
        
    }
    
    static {
        createLogger();
    }
    
    private static void createLogger() {
        try {
            ILoggerFactory iLoggerFactory = LoggerFactory.getILoggerFactory();
            Class<? extends ILoggerFactory> classType = iLoggerFactory.getClass();
            if (classType.getName().equals("org.slf4j.impl.Log4jLoggerFactory")) {
                Class<?> DOMConfigurator = null;
                Object DOMConfiguratorObj = null;
                DOMConfigurator = Class.forName("org.apache.log4j.xml.DOMConfigurator");
                DOMConfiguratorObj = DOMConfigurator.newInstance();
                URL log4j_config_file = ClientLogger.class.getClassLoader().getResource(
                    MixAll.TSP_CLIENT_LOG4J_PAHT);
                Method configure = DOMConfiguratorObj.getClass().getMethod("configure", URL.class);
                configure.invoke(DOMConfiguratorObj, log4j_config_file);
            } else if (classType.getName().equals("ch.qos.logback.classic.LoggerContext")) {
                Class<?> joranConfigurator = null;
                Class<?> context = Class.forName("ch.qos.logback.core.Context");
                Object joranConfiguratoroObj = null;
                joranConfigurator = Class.forName("ch.qos.logback.classic.joran.JoranConfigurator");
                joranConfiguratoroObj = joranConfigurator.newInstance();
                Method setContext = joranConfiguratoroObj.getClass().getMethod("setContext",
                    context);
                setContext.invoke(joranConfiguratoroObj, iLoggerFactory);
                URL logback_config_file = ClientLogger.class.getClassLoader().getResource(
                    MixAll.TSP_CLIENT_LOGBACK_PAHT);
                Method doConfigure = joranConfiguratoroObj.getClass().getMethod("doConfigure",
                    URL.class);
                doConfigure.invoke(joranConfiguratoroObj, logback_config_file);
            }
        } catch (Exception e) {
            System.err.println(e);
        }
    }
    
    public static Logger getLogger(String loggerName) {
        return LoggerFactory.getLogger(loggerName);
    }
    
}
